package com.alvinlkk.basic.chapter21;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class AttemptLocking {
    
    private Lock lock = new ReentrantLock();
    
    public void untimed() {
        boolean captured = lock.tryLock();
        try {
            System.out.println("tryLock(): " + captured);
        } finally {
            if(captured) {
                lock.unlock();
            }
        }
    }
    
    public void timed() {
        boolean captured = false;
        try {
            captured = lock.tryLock(2, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        
        try {
            System.out.println("tryLock(2, seconds): " + captured);
        } finally {
            if(captured) {
                lock.unlock();
            }
        }
    }
    
    public static void main(String[] args) throws InterruptedException {
        final AttemptLocking al = new AttemptLocking();
        al.untimed();
        al.timed();
        
        new Thread() {
            {setDaemon(true);}
            public void run() {
                al.lock.lock();
                System.out.println("acqired");
            };
        }.start();
        
        TimeUnit.SECONDS.sleep(2);
        Thread.yield();
        al.untimed();
        al.timed();
    }
}
